﻿using System;
using System.Collections.Generic;
using System.Text;

using ECCIT;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DALFactory;
using OgilvyOne.UKNow.Model;
using System.Xml.Linq;
using OgilvyOne.UKNow.Utility;

namespace OgilvyOne.UKNow.BLL
{
    public class EventsVideoBLL
    {
        public int LanguageID { get; set; }
        public EventsVideoBLL(int langID)
        {
            this.LanguageID = langID;
        }

        public ResultInfo Save(EventsVideoModel info)
        {
            IDALEventsVideo use = EventsVideoDALFactory.Create(LanguageID);
            info.LanguageID = LanguageID;
            return use.Save(info);
        }
        //删除单个视频的数据
        public ResultInfo delVideo(int eid)
        {
            IList<int?> list = new List<int?>();
            list.Add(eid);
            IDALEventsVideo use = EventsVideoDALFactory.Create(LanguageID);
            return use.Delete(list);
        }
        public ResultInfo DeleteVideosByEventsID(int eventsID)
        {
            return EventsVideoDALFactory.Create(LanguageID).DeleteByEventsID(null, eventsID);
        }

        //根据视频id 获取该视频信息
        public EventsVideoModel selVideo(int id)
        {
            IDALEventsVideo use = EventsVideoDALFactory.Create(LanguageID);
            return use.GetEventsVideoModelByID(id);
        }
        /*修改人：穆凤凤
    * 修改时间：2012-01-02
    * 修改内容：
    *      添加方法,修改某个活动视频的发布状态
    *      有mff注释的为本次修改的代码
    */
        //mff
        //修个单个视频的发布状态
        public void modPubOne(int eid)
        {
            List<int?> list = new List<int?>();
            list.Add(eid);

            IDALEventsVideo use = EventsVideoDALFactory.Create(LanguageID);

            EventsVideoModel model = use.GetEventsVideoModelByID(eid); //查看该视频的当前发布状态
            if (model.State > 0) //该视频的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该视频的state为0 不发布 (传递参数 eid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该视频的state为1 发布     (传递参数 eid 和 int=1)
            }
        }
        //mff
        public IList<EventsVideoModel> GetAllEventsVideoModels(int? eventsID)
        {
            IDALEventsVideo use = EventsVideoDALFactory.Create(LanguageID);
            return use.GetAllEventsVideoModels(eventsID);
        }
        public PagingList GetEventsVideoModelsByKeywords(string keyword, int pageSize, int currentPageIndex)
        {
            PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currentPageIndex, pageSize, null);
            IDALEventsVideo use = EventsVideoDALFactory.Create(LanguageID);
            return use.GetEventsVideoModelsByKeywords(keyword, pagingParams);
        }

        public IList<EventsVideoModel> GetPublishedEventsVideoModels(int? eventsID)
        {
            return EventsVideoDALFactory.Create(LanguageID).GetPublishedEventsVideoModels(eventsID);
        }

        public IList<EventsVideoModel> GetPublishedGalleryVideoModelsByEventsID(int? eventsID)
        {
            return EventsVideoDALFactory.Create(LanguageID).GetPublishedGalleryVideoModelsByEventsID(eventsID);
        }

        public IList<EventsVideoModel> GetPublishedGalleryVideoModels()
        {
            return EventsVideoDALFactory.Create(LanguageID).GetPublishedGalleryVideoModels();
        }

        private string XMLBuild(ImgXML imgxml)
        {

            XDocument Employee = new XDocument(new XDeclaration("1.0", "utf-8", "true"),
                                    new XElement("Video",

                                                            new XAttribute("id", imgxml.Id),
                                                            new XAttribute("url", imgxml.Url!=null?imgxml.Url:""),
                                                            new XAttribute("Eventid", imgxml.Eventid!=null?imgxml.Eventid:""),
                                                            new XAttribute("EventName", imgxml.EventName!=null?imgxml.EventName:""),
                                                            new XAttribute("eventStart", imgxml.EventDateStart!=null?imgxml.EventDateStart:""),
                                                             new XAttribute("eventEnd", imgxml.EventDateEnd != null ? imgxml.EventDateEnd : ""),
                                                            new XAttribute("eventCity", imgxml.Eventcity!=null?imgxml.Eventcity:""),
                                                            new XAttribute("eventArt_Form", imgxml.EventArt_Form!=null?imgxml.EventArt_Form:"")
                                                            ));

            return Employee.ToString();

         
        }
        public string ReXml(int langId)
        {

            ImgXML imgxml = null;
            IList<EventsVideoModel> iLeim = EventsVideoDALFactory.Create(langId).GetAllPublishedEventsRecoVideoModels();
            StringBuilder stb = new StringBuilder();
           
            foreach (EventsVideoModel eim in iLeim)
            {
                EventsModel imX = GetMod(langId, Convert.ToInt32(eim.EventsID));
                if (imX != null)
                {
                    imgxml = new ImgXML();
                    imgxml.Url = eim.Path;
                    imgxml.Id = eim.ID.ToString();
                    imgxml.Eventid = eim.EventsID.ToString();
                    imgxml.EventName = imX.Title;
                    imgxml.EventDateStart = imX.StartTime.ToString();
                    imgxml.EventDateStart = imX.EndTime.ToString();
                    imgxml.Eventcity = (
                    langId == 2052 ? DictionaryUtil.GetCityByID(Convert.ToInt32(imX.City)).Cnname : DictionaryUtil.GetCityByID(Convert.ToInt32(imX.City)).Enname);
                    stb.Append(XMLBuild(imgxml));
                }


            }
            return stb.Append("</Videos>").Insert(0, String.Format("<Videos count='{0}'>", iLeim.Count)).ToString();



         
        }
        private EventsModel GetMod(int langID,int id)
        {

            return EventsDALFactory.Create(langID).GetEventsModelByID(id);
          

        }

        /// <summary>
        /// 插入视频信息
        /// </summary>
        /// <param name="vid"></param>
        /// <param name="vNameOld"></param>
        /// <param name="vNamePath"></param>
        public  void InsertFlvBase(string vid, string vNameOld, string vNamePath)
        {
             EventsVideoDALFactory.Create(LanguageID).InsertFlvBase(vid, vNameOld, vNamePath);
        }

    }
}
